package main

import (
	"fmt"
	"local/datastructure/queue"
)

type TreeNode struct {
	Value int
	Left  *TreeNode
	Right *TreeNode
}

func main() {
	root := TreeNode{Value: 1, Left: nil, Right: nil}
	left := TreeNode{Value: 2, Left: nil, Right: nil}
	right := TreeNode{Value: 3, Left: nil, Right: nil}
	root.Left = &left
	root.Right = &right
	levelorder(&root)
}

func preorder(root *TreeNode) {
	if root == nil {
		return
	}

	fmt.Println(root.Value)
	preorder(root.Left)
	preorder(root.Right)
}

func midorder(root *TreeNode) {
	if root == nil {
		return
	}

	preorder(root.Left)
	fmt.Println(root.Value)
	preorder(root.Right)
}

func tailorder(root *TreeNode) {
	if root == nil {
		return
	}

	preorder(root.Left)
	preorder(root.Right)
	fmt.Println(root.Value)
}

func levelorder(root *TreeNode) {
	if root == nil {
		return
	}

	q := queue.NewQueue()
	q.Push(root)
	for !q.IsEmpty() {
		a, err := q.Pop()
		if err == nil {
			fmt.Println(a.(*TreeNode).Value)
			if a.(*TreeNode).Left != nil {
				q.Push(a.(*TreeNode).Left)
			}
			if a.(*TreeNode).Right != nil {
				q.Push(a.(*TreeNode).Right)
			}
		}
	}
}
